package cn.itcalvin.sncoal.cth.controller;

import cn.itcalvin.sncoal.cth.aspect.LogAnnotation;
import cn.itcalvin.sncoal.cth.aspect.RoleAnnotation;
import cn.itcalvin.sncoal.cth.component.MinioUtil;
import cn.itcalvin.sncoal.cth.mapper.cl.ClMapper;
import cn.itcalvin.sncoal.cth.pojo.common.ReqLong;
import cn.itcalvin.sncoal.cth.pojo.common.ReqStr;
import cn.itcalvin.sncoal.cth.pojo.req.minio.ReqUploadFile;
import cn.itcalvin.sncoal.cth.pojo.req.pc.ReqPfZbModify;
import cn.itcalvin.sncoal.cth.pojo.req.pc.ReqPfZbSave;
import cn.itcalvin.sncoal.cth.pojo.req.pf.ReqZbDetailsCl;
import cn.itcalvin.sncoal.cth.pojo.response.pc.RespPfZbDetails;
import cn.itcalvin.sncoal.cth.pojo.response.pf.RespZbDetailsCl;
import cn.itcalvin.sncoal.cth.service.ClService;
import cn.itcalvin.sncoal.cth.service.InfoPfSystemZbDetailsService;
import cn.itcalvin.vo.Message;
import cn.itcalvin.vo.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "草料二维码溯源管理")
@RestController
@RequestMapping("/api/cl")
public class ClController {

    @Resource
    private ClService clService;


    @ApiOperation("获取草料指标溯源数据")
    @PostMapping("getZbDetailsClList")
    @LogAnnotation(value = "获取草料指标溯源数据")
    public Message<List<RespZbDetailsCl>> getZbDetailsClList(@RequestBody @Valid ReqZbDetailsCl reqZbDetailsCl){
        List<RespZbDetailsCl> zbDetailsClList=new ArrayList<>();
        if(reqZbDetailsCl.getZbDetailsCodeId()!=null) {
            zbDetailsClList = clService.getZbDetailsClList(reqZbDetailsCl.getZbDetailsCodeId());
        }
        List<RespZbDetailsCl> listPc=infoPfSystemZbDetailsService.getZbDetailsResourcePcList(reqZbDetailsCl);
        List<RespZbDetailsCl> allList=new ArrayList<>();
        allList.addAll(listPc);
        allList.addAll(zbDetailsClList);
        allList.sort((o1, o2) -> o2.getRecordTime().compareTo(o1.getRecordTime()));
        return MessageUtil.success(allList);
    }


    @Resource
    MinioUtil minioUtil;

    @Value("${cth.minioUrl}")
    private String minioUrl;

    @Resource
    InfoPfSystemZbDetailsService infoPfSystemZbDetailsService;


    @PostMapping("/uploadMinePfZb")
    @ApiOperation(value = "上传PC端佐证材料上传")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "上传PC端佐证材料上传")
    public Message uploadMinePfZb(@RequestParam(value = "file") MultipartFile file,
                                  @RequestParam(value = "pfId") Long pfId
            , @RequestParam(value = "mineId") Long mineId
            , @RequestParam(value = "zbDetailsId") Long zbDetailsId){
        String fileUrl = minioUtil.upload("minePfZb/"+mineId+"/"+pfId+"/"+zbDetailsId, file,false);
        return MessageUtil.success(minioUrl+fileUrl);
    }

    @PostMapping("/removeMinePfZb")
    @ApiOperation(value = "删除PC端佐证材料上传")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "删除PC端佐证材料上传")
    public Message removeMinePfZb(@RequestBody @Valid ReqUploadFile reqUploadFile){
        return minioUtil.remove(reqUploadFile.getParentPath().replace(minioUrl,""))?MessageUtil.success():MessageUtil.error("删除失败");
    }

    @PostMapping("/saveMinePfZb")
    @ApiOperation(value = "保存PC端佐证材料上传")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "保存PC端佐证材料上传")
    public Message saveMinePfZb(@RequestBody @Valid ReqPfZbSave reqPfZbSave){
        return infoPfSystemZbDetailsService.saveMinePfZb(reqPfZbSave);
    }

    @PostMapping("/modifyMinePfZb")
    @ApiOperation(value = "修改PC端佐证材料上传")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "修改PC端佐证材料上传")
    public Message modifyMinePfZb(@RequestBody @Valid ReqPfZbModify reqPfZbModify){
        return infoPfSystemZbDetailsService.modifyMinePfZb(reqPfZbModify);
    }

    @PostMapping("/deleteMinePfZb")
    @ApiOperation(value = "删除PC端佐证材料上传")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "删除PC端佐证材料上传")
    public Message deleteMinePfZb(@RequestBody @Valid ReqLong reqLong){
        return infoPfSystemZbDetailsService.deleteMinePfZb(reqLong);
    }

    @PostMapping("/getMinePfZbById")
    @ApiOperation(value = "获取PC端佐证材料上传详情")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "获取PC端佐证材料上传详情")
    public Message<RespPfZbDetails> getMinePfZbById(@RequestBody @Valid ReqLong reqLong){
        return infoPfSystemZbDetailsService.getMinePfZbById(reqLong);
    }
}
